package com.shop.shoporder.wapController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.shoporder.common.MessageConstants;
import com.shop.shoporder.common.Response;
import com.shop.shoporder.common.StringUtil;
import com.shop.shoporder.entity.*;
import com.shop.shoporder.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("/wap")
public class GroupbuyGoodController {

    @Autowired
    GroupbuyGoodService groupbuyGoodService;
    @Autowired
    WapGoodsDetailsService wapGoodsDetailsService;
    @Autowired
    GroupbuyService groupbuyService;
    @Autowired
    WapClassificationService wapClassificationService;
    @Autowired
    GroupbuyFinaceService groupbuyFinaceService;
Logger logger=LoggerFactory.getLogger(GroupbuyGoodController.class);
    //查询团购商品分类一级分类
    @ResponseBody
    @RequestMapping("/getGroupbuyGoodsClassfy")
    public Response getGroupbuyGoods(HttpServletRequest request) {
        List<WapClassificationToData> classificationList = null;
        try {
            classificationList = wapClassificationService.selectOneLevelByPId();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Response(MessageConstants.MSG_SUCCESS, classificationList, 0, "", request);
    }


    /**
     * 根据一级分类ID查询产品信息
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/getGroupbuyGoodByClassfyId")
    public Response getGroupbuyGoodByClassfyId(HttpServletRequest request) {
        String classfyId = request.getParameter("classfyId");
        int classfyIds = 0;
        List<GroupbuyGoodInfo> groupbuyGoodInfoList = null;
        PageInfo<GroupbuyGoodInfo> pageInfo = null;
        int page = 1;
        int limit = 5;
        String pages = request.getParameter("page");
        String limits = request.getParameter("limit");
        if (pages != null && pages != "") {
            page = Integer.parseInt(pages);
        }
        if (limits != null && limits != "") {
            limit = Integer.parseInt(limits);
        }
        if (classfyId == null || classfyId == "" || !StringUtil.isNumeric(classfyId)) {

            //调用列表

            PageHelper.startPage(page, limit);
            groupbuyGoodInfoList = groupbuyGoodService.getGroupbuyGooods();
            pageInfo = new PageInfo<>(groupbuyGoodInfoList);
        } else {
            //根据分类ID 查询
            classfyIds = Integer.parseInt(classfyId);
            PageHelper.startPage(page, limit);
            groupbuyGoodInfoList = groupbuyGoodService.getGroupbuyGoodByClassfyId(classfyIds);
            pageInfo = new PageInfo<>(groupbuyGoodInfoList);
        }


        return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), pageInfo.getTotal(), "", request);
    }


    /**
     * 团购商品详情
     */
    @ResponseBody
    @RequestMapping("/getGroupbuyGoodsDetail")
    public Response getGroupbuyGoodsDetail(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        String groupbuygoodIds = request.getParameter("groupbuygoodId");
        int groupbuygoodId = 0;
        int goodId = 0;
        int specId = 0;
        if (groupbuygoodIds != null && groupbuygoodIds != "") {
            groupbuygoodId = Integer.parseInt(groupbuygoodIds);
        }
        //查询团购商品
        GroupbuyGood groupbuyGood = null;
        groupbuyGood = groupbuyGoodService.selectByPrimaryKey(groupbuygoodId);
        if (groupbuyGood != null) {
            goodId = groupbuyGood.getGoodsId();
            specId = groupbuyGood.getSpecId();
        } else {
            return new Response(MessageConstants.UNGROUPBUY, groupbuyGood, 0, "", request);
        }
        List<GroupbuyGood> groupbuyGoodList = null;
        groupbuyGoodList = groupbuyGoodService.getGroupbuyGoodListByGoodId(goodId);


        //商品详情
        String[] goodsPicArr = null;//产品图组
        String[] goodsDetialPicArr = null;//产品详情图
        GroupbuyGoodDetail groupbuyGoodDetail = wapGoodsDetailsService.getGroupbuyGoodDetail(goodId);
        if (groupbuyGoodDetail != null) {
            goodsPicArr = groupbuyGoodDetail.getPiclist().split(",");
            goodsDetialPicArr = groupbuyGoodDetail.getPicdetails().split(",");
            groupbuyGoodDetail.setPicGroup(goodsPicArr);
            groupbuyGoodDetail.setPicDetail(goodsDetialPicArr);
        }


        //查询该团购商品的发起团购数和参与人数
        GroupbuyUserInfo groupbuyUserInfo = null;
        int participaters = 0;
        List<GroupbuyUserInfo> groupbuyUserInfoList = new ArrayList<>();
        List<GroupbuyInfo> groupbuyInfos = groupbuyService.getGroupbuyListByGroupbuyGoodId(groupbuygoodId);
        for (int i = 0; i < groupbuyInfos.size(); i++) {
            groupbuyUserInfo = new GroupbuyUserInfo();
            groupbuyUserInfo.setUserNoId(groupbuyInfos.get(i).getUserNoId());
            groupbuyUserInfo.setUserNo(groupbuyInfos.get(i).getUserNo());
            groupbuyUserInfo.setUserNick(groupbuyInfos.get(i).getUserNick());
            groupbuyUserInfo.setInitiateStarttime(groupbuyInfos.get(i).getInitiateStarttime());
            groupbuyUserInfo.setGroupbuyEndtime(groupbuyInfos.get(i).getGroupbuyEndtime());
            groupbuyUserInfo.setGroupbuyId(groupbuyInfos.get(i).getGroupbuyId());
            participaters += groupbuyInfos.get(i).getParticipaters().size();
            groupbuyUserInfo.setParticipaterCount(groupbuyInfos.get(i).getParticipaters().size() + 1);

            groupbuyUserInfoList.add(groupbuyUserInfo);
        }

        //单团最大团购人数
        GroupbuyFinace groupbuyFinace = null;
        int maxOneGroupbuyCount = 0;
        groupbuyFinace = groupbuyFinaceService.getMaxGroupbuyCount();
        if (groupbuyFinace != null) {
            maxOneGroupbuyCount = groupbuyFinace.getGroupbuyCount();
        }


        map.put("groupbuyCount", groupbuyInfos.size() + participaters);//团购的总人数
//        map.put("goodSpec", goodSpec1);//商品详情信息
        map.put("groupbuyGoodDetail", groupbuyGoodDetail);//商品详情信息
//        map.put("groupbuyGood", groupbuyGood);//团购商品
        map.put("groupbuyGoodList", groupbuyGoodList);//团购产品的列表，不同规格
        map.put("groupbuyUserInfoList", groupbuyUserInfoList);//团购的团主信息及参与人数
        map.put("alreadyGroupbuyCount", groupbuyInfos.size());
        map.put("maxOneGroupbuyCount", maxOneGroupbuyCount);
        return new Response(MessageConstants.MSG_SUCCESS, map, 0, "", request);

    }

    /**
     * 根据商品ID和规格ID，获取团购商品的信息
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/getGroupbuyGood")
    public Response getGroupbuyGood(HttpServletRequest request) {
        int goodId = 0;
        int specId = 0;
        String goodIds = request.getParameter("goodId");
        String specIds = request.getParameter("specId");
        if (goodIds != null && goodIds != "" && !StringUtil.isNumeric(goodIds)) {
            goodId = Integer.parseInt(goodIds);
        }
        if (specIds != null && specIds != "" && !StringUtil.isNumeric(specIds)) {
            specId = Integer.parseInt(specIds);
        }
        GroupbuyGood groupbuyGood = null;
        if (goodId != 0 && specId != 0) {
            groupbuyGood = groupbuyGoodService.getGroupbuyGoodByGoodIdAndSpecId(goodId, specId);
        }

        return new Response(MessageConstants.MSG_SUCCESS, groupbuyGood, 0, "", request);
    }


}
